package com.sdu.game.wbr.game_play;

import com.sdu.game.mapper.PlayerbMapper;
import com.sdu.game.pojo.Playerb;
import com.sdu.game.pojo.PlayerbExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@CrossOrigin
public class ActFunction {

    @Autowired
    PlayerbMapper playerbMapper;

    public ActFunction(){

    }

    public static int voted_seat = 0;

    public String WolfVoteAction(String content){

        int voted_seat = Integer.parseInt(content);   // 被投票的玩家的座位号
        String result = "2,"+voted_seat;  //  回传 2+被投票玩家的座位号

        return result;

    }

    public String Action(ConcurrentHashMap<String, String> map){

        String Data_result = "";  // 返回给前端的信息

        System.out.println("voted_seat : " + voted_seat);

        for(String key:map.keySet()){
            System.out.print("Map : ");
            System.out.print("  Key : "+key);
            System.out.print("  Value : "+map.get(key));
        }

        // userVoteList  <String : 玩家ID, String : message字符串>
//        Object obj = map.keySet().toArray()[0];
        Object obj = map.keySet().toArray()[map.keySet().toArray().length-1];

        String user_ID = obj.toString();
        System.out.println("Action - user_ID : "+user_ID);

//        // TODO 使用user_ID在数据库中查询到此人的座位号seat
        int userID=Integer.valueOf(user_ID);

        PlayerbExample playerbExample = new PlayerbExample();
        PlayerbExample.Criteria criteria = playerbExample.or();
        criteria.andUserIdEqualTo(userID);
        List<Playerb> list = playerbMapper.selectByExample(playerbExample);
        Playerb playerb=list.get(0);
        int seat=playerb.getSeatNumber();

//        // int seat = 数据库操作方法(user_ID);
//        int seat = 0;

        String message = map.get(user_ID);
//        userVoteList.remove(user_ID);  // 移除该记录
        // 进行命令判断
        String[] mes_list = message.split(",");
        int order = Integer.parseInt(mes_list[0]);

        System.out.println("order : "+order);

        switch (order){
            case 2:  // 狼人投票结束
            {
                // 得到被票选出的玩家座位号
                int result = GetWolfVoteResult(map);
                voted_seat = result;  // 保存被票出来的玩家的座位号

                Data_result = "3,"+result;

            }
            break;

            case 3:  // 女巫救人阶段
            {
                // 跳过修改数据库（将玩家设为死亡）的阶段
                Data_result = "4";
                voted_seat = 0;

                map.remove(user_ID);

            }
            break;

            case 4:  // 女巫投毒
            {
                Data_result = "4";
                map.remove(user_ID);
            }
            break;

            case 5:  // 女巫无操作
            {
                // TODO 数据库 通过voted_seat和RoomID将该玩家设为死亡

                int room_id=1000;

                PlayerbExample playerbExample1=new PlayerbExample();
                PlayerbExample.Criteria criteria1 = playerbExample1.or();
                criteria1.andSeatNumberEqualTo(voted_seat);
                criteria1.andRoomIdEqualTo(room_id);
                List<Playerb> list1 = playerbMapper.selectByExample(playerbExample);
                Playerb playerb1 = list.get(0);

                //将用户置为死亡
                playerb1.setAli(0);//1是活着0是死亡

                int updateRow1=playerbMapper.updateByPrimaryKeySelective(playerb1);
                System.out.println(updateRow1);//是否修改成功

                map.remove(user_ID);
                Data_result = "4";

            }
            break;

            case 6:  // 预言家验人
            {
                map.remove(user_ID);
                int checking_seat = Integer.parseInt(mes_list[1]);

                String chara = ProphetAction(checking_seat);
                Data_result = "5,"+checking_seat+"号位为"+chara;

                map.remove(user_ID);

            }
            break;


            case 8:  // 白天投票
            {
                String result = GetVillagersVoteResult(map);
                System.out.println(result);

            }
            break;

            case 9:  // 进入白天的信号
            {
                Data_result = "6,"+voted_seat+",";
                if(voted_seat==0){
                    Data_result+="昨天是平安夜。";
                }
                else{
                    Data_result+="昨天"+voted_seat+"号玩家死亡。";
                }

                map.remove(user_ID);
            }

        }

        System.out.println(Data_result);
        return Data_result;

    }

    /**
     * 狼人投票
     * @return
     */
    private int GetWolfVoteResult(ConcurrentHashMap<String, String> map){

        // 新建Vote类
        WolfVote v = new WolfVote();

        // 遍历userVoteList的信息(同白天）
        for (String userID : map.keySet()){
            // userID 所投票用户编号
            // userVoteList.get(userID)   message信息

            String str = map.get(userID);
            System.out.println("aaaa userID : "+userID);

            String[] str_list = str.split(",");
            System.out.println("str_list[0] : "+str_list[0]);

            if(str_list[0].equals("2")){
                // 命令2 是狼人投票
                // TODO 这里离需要进行数据库查询：在Player表里通过RoomID和userID查询出所投票用户的座位号user_seat

                int user_id=Integer.valueOf(userID);
                int room_id=1000;

                PlayerbExample playerbExample = new PlayerbExample();
                PlayerbExample.Criteria criteria = playerbExample.or();
                criteria.andUserIdEqualTo(user_id);
                criteria.andRoomIdEqualTo(room_id);
                List<Playerb> list = playerbMapper.selectByExample(playerbExample);
                Playerb playerb = list.get(0);

                int user_seat = playerb.getSeatNumber();


                System.out.println("谁被投了 : "+Integer.parseInt(str_list[1]));
                // 在Vote类里进行投票统计
                v.VotePlayer(user_seat, Integer.parseInt(str_list[1]));

                map.remove(userID);
            }

        }

        // 查询Vote结果
        // result是被投票选出的玩家的座位号
        // 如果result为0，则表示出现多位最高票数的玩家，需要重新投票
        int result = v.CheckResults();

        if(result == 0){
            // TODO 重新投票
            System.out.println("重新投票");

        }
        else{
            // TODO 杀死选中玩家
            System.out.println(result +"号位的玩家被杀死。");

        }

        return result;


    }


    /**
     * 白天全体投票
     * @return
     */
    private String GetVillagersVoteResult(ConcurrentHashMap<String, String> map){

        String Data_result = "";

        // 新建Vote类
        VillageVote v = new VillageVote();

        // 遍历userVoteList的信息
        for (String userID : map.keySet()){
            // userID 所投票用户编号
            // userVoteList.get(userID)   message信息

            String str = map.get(userID);
            String[] str_list = str.split(",");

            if(str_list[0].equals("8")) {
                // 命令8 是狼人投票

                // TODO 这里离需要进行数据库查询：在Player表里通过RoomID和userID查询出所投票用户的座位号user_seat
                int user_id=Integer.valueOf(userID);
                int room_id=1000;

                PlayerbExample playerbExample = new PlayerbExample();
                PlayerbExample.Criteria criteria = playerbExample.or();
                criteria.andUserIdEqualTo(user_id);
                criteria.andRoomIdEqualTo(room_id);
                List<Playerb> list = playerbMapper.selectByExample(playerbExample);
                Playerb playerb = list.get(0);

                int user_seat=playerb.getSeatNumber();

//                int user_seat = 0;
//                int user_seat = Integer.parseInt(userID);

                System.out.println("谁被投了 : " + Integer.parseInt(str_list[1]));
                // 在Vote类里进行投票统计
                v.VotePlayer(user_seat, Integer.parseInt(str_list[1]));

                map.remove(userID);
            }

        }

        // 查询Vote结果
        // result是被投票选出的玩家的座位号
        // 如果result为0，则表示出现多位最高票数的玩家，需要重新投票
        int result = v.CheckResults();

        if(result == 0){
            // TODO 重新投票
            System.out.println("重新投票");

            for (int seat : v.VoteResultdMap.keySet()){
                Data_result+=seat;
                Data_result+="号玩家： ";
                Data_result+=v.VoteResultdMap.get(seat);
                Data_result+="票  ";
            }

            Data_result+="<br>";

            Data_result+= result + "号玩家死亡。";

        }
        else{
            // TODO 杀死选中玩家


            System.out.println(result +"号位的玩家被票死。");
            Data_result = "9";

        }

        // 需要投票的详细信息，请调用v.VoteResultdMap


        return Data_result;
    }


    private String ProphetAction(int checking_seat){

        // TODO 数据库操作，通过checking_seat座位号和房间号，查出该玩家是什么身份，返回身份character
        int user_id=1;
        int room_id=1000;

        PlayerbExample playerbExample = new PlayerbExample();
        PlayerbExample.Criteria criteria = playerbExample.or();
        criteria.andUserIdEqualTo(user_id);
        criteria.andRoomIdEqualTo(room_id);
        List<Playerb> list = playerbMapper.selectByExample(playerbExample);
        Playerb playerb = list.get(0);

        int characternum=playerb.getCha();//使用数字来表示角色
        String character;

        switch (characternum){
            case 1:
                character="wolf";
                break;
            case 2:
                character="witch";
                break;
            case 3:
                character="villager";
                break;
            default:
                character="prophet";


        }


//        String character = "wolf";
        String result = "";

        switch (character){
            case "wolf":
                result = "狼人";
                break;
            case "villager":
                result = "村民";
                break;
            case "witch":
                result = "女巫";
                break;
            case "prophet":
                result = "预言家";
                break;
            default:
                result = "————";

        }

        return result;

    }

}
